package com.bw.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.core.result.ResponseResult;
import com.bw.core.utils.StringUtils;
import com.bw.core.vo.FindOneBankResVo;
import com.bw.user.mapper.TbBankInfoMapper;
import com.bw.user.pojo.TbBankCardAuth;
import com.bw.user.mapper.TbBankCardAuthMapper;
import com.bw.user.pojo.TbBankInfo;
import com.bw.user.pojo.TbUser;
import com.bw.user.service.TbBankCardAuthService;
import com.bw.user.vo.BankCardJson;
import com.bw.user.vo.BankCardVo;
import com.bw.user.vo.BankYzmJSon;
import com.bw.user.vo.TiedCardReqVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.bw.core.creditStatus.BindCardStatus.SUCCESS_AUTH;

/**
 * <p>
 * 银行卡认证表&信息 服务实现类
 * </p>
 *
 * @author lv
 * @since 2021-03-23
 */
@Service
public class TbBankCardAuthServiceImpl extends ServiceImpl<TbBankCardAuthMapper, TbBankCardAuth> implements TbBankCardAuthService {

    @Autowired
    private TbBankCardAuthMapper bankCardAuthMapper;

    @Autowired
    private TbBankInfoMapper bankInfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Override
    public ResponseResult bankcard(BankCardVo bankcardVo) {
        if(StrUtil.isEmpty(bankcardVo.getBankProtocolNo())){
            return ResponseResult.error("绑卡协议号为空");
        }

        if(StrUtil.isEmpty(bankcardVo.getYzm())){
            return ResponseResult.error("请输入验证码");
        }

        if(StrUtil.isEmpty(bankcardVo.getReservedPhone())){
            return ResponseResult.error("预留手机号为空");
        }

        if(StrUtil.isEmpty(bankcardVo.getReservedName())){
            return ResponseResult.error("姓名为空");
        }
        if(StrUtil.isEmpty(bankcardVo.getBankCardNo())){
            return ResponseResult.error("银行卡号为空");
        }

        String s = (String) redisTemplate.opsForValue().get(bankcardVo.getReservedPhone());

        if (!s.equals(bankcardVo.getYzm())){
            return ResponseResult.error("验证码错误,请重试!");
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("bankProtocolNo",bankcardVo.getBankProtocolNo());
        map.put("yzm",bankcardVo.getYzm());

        String post = HttpUtil.post("http://localhost:8080/api/common/bindCardConfirm", map);
        BankCardJson bankCardJson = JSONObject.parseObject(post, BankCardJson.class);

        if(ObjectUtil.isNull(bankCardJson)){
            return ResponseResult.error("系统繁忙!");
        }

        if(bankCardJson.getMessage().equals("失败")){
            return ResponseResult.error("绑卡失败!");
        }

        QueryWrapper<TbBankCardAuth> wrapper = new QueryWrapper<>();
        wrapper.eq("bank_card_no",bankcardVo.getBankCardNo());
        List<TbBankCardAuth> bankCardAuths = bankCardAuthMapper.selectList(wrapper);

        if (CollUtil.isEmpty(bankCardAuths)){
            return ResponseResult.error("绑卡异常!");
        }

        TbBankCardAuth tbBankCardAuth = bankCardAuths.get(0);
        tbBankCardAuth.setBankProtocolNo(bankcardVo.getBankProtocolNo());
        tbBankCardAuth.setCreateTime(new Date());
        tbBankCardAuth.setBandCarStatus(1);

        bankCardAuthMapper.updateById(tbBankCardAuth);

        return ResponseResult.success("绑定成功");

    }

    @Override
    public ResponseResult banksms(TiedCardReqVo tiedCardReqVo) {

        if(StrUtil.isEmpty(tiedCardReqVo.getUserIdentityNo())){
            return ResponseResult.error("请输入身份证号");
        }

        if(StrUtil.isEmpty(tiedCardReqVo.getReservedPhone())){
            return ResponseResult.error("预留手机号为空");
        }

        if(StrUtil.isEmpty(tiedCardReqVo.getUserName())){
            return ResponseResult.error("姓名为空");
        }
        if(StrUtil.isEmpty(tiedCardReqVo.getBankCardNo())){
            return ResponseResult.error("银行卡号为空");
        }

        if(ObjectUtil.isNull(tiedCardReqVo.getBandId())){
            return ResponseResult.error("请选择银行");
        }


        QueryWrapper<TbBankInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id",tiedCardReqVo.getBankCardNo());
        List<TbBankInfo> bankInfos = bankInfoMapper.selectList(wrapper);

        if(CollUtil.isEmpty(bankInfos)){
            return ResponseResult.error("无数据");
        }

        QueryWrapper<TbBankCardAuth> wrapper1 = new QueryWrapper<>();
        wrapper.eq("bankCardNo",tiedCardReqVo.getBankCardNo());
        List<TbBankCardAuth> bankCardAuths = bankCardAuthMapper.selectList(wrapper1);


        if(ObjectUtil.isNull(bankCardAuths)){
            return ResponseResult.error("该银行卡已绑定!");
        }


        TbBankInfo tbBankInfo = bankInfos.get(0);

        HashMap<String,Object> map = new HashMap<>();
        //姓名
        map.put("name",tiedCardReqVo.getUserName());
        //身份证
        map.put("cardId",tiedCardReqVo.getUserIdentityNo());
        //银行卡号
        map.put("accNo",tiedCardReqVo.getBankCardNo());
        //银行名称
        map.put("bankName",tbBankInfo.getBankName());
        //银行简称
        map.put("bankShortName",tbBankInfo.getBankShortName());
        //银行预留手机号
        map.put("reserveMobile",tiedCardReqVo.getReservedPhone());

        String post = HttpUtil.post("http://localhost:8080/api/common/bindCardSms", map);
        BankYzmJSon bankYzmJSon = JSONObject.parseObject(post, BankYzmJSon.class);

        if(ObjectUtil.isEmpty(bankYzmJSon)){
            return ResponseResult.error("系统繁忙!");
        }

        TbBankCardAuth tbBankCardAuth = new TbBankCardAuth();
        tbBankCardAuth.setReservedName(tiedCardReqVo.getUserName());
        tbBankCardAuth.setBankCardNo(tiedCardReqVo.getBankCardNo());
        tbBankCardAuth.setReservedPhone(tiedCardReqVo.getReservedPhone());
        tbBankCardAuth.setBandCarStatus(0);
        tbBankCardAuth.setBandId(tbBankInfo.getId());
        tbBankCardAuth.setBandName(tbBankInfo.getBankName());
        tbBankCardAuth.setBankShortName(tbBankInfo.getBankShortName());
        tbBankCardAuth.setIsDelete(0);
        tbBankCardAuth.setCreateTime(new Date());
        tbBankCardAuth.setUpdateTime(new Date());
        tbBankCardAuth.setBankProtocolNo(bankYzmJSon.getData().getBindCardId());
        tbBankCardAuth.setBandCarStatus(SUCCESS_AUTH);
        /*redisTemplate.boundValueOps()*/

        bankCardAuthMapper.insert(tbBankCardAuth);

        redisTemplate.opsForValue().set(tiedCardReqVo.getReservedPhone(),bankYzmJSon.getData().getCode(),5, TimeUnit.MINUTES);

        String s ="绑卡验证码: "+bankYzmJSon.getData().getCode();
        System.out.println(s);
        amqpTemplate.convertAndSend("BankCard_size",s);

        return ResponseResult.success(bankYzmJSon.getData().getBindCardId());

    }



    //汪兴达，获取用户银行卡信息
    @Override
    public FindOneBankResVo findBankResVo(Long userId) {

        return null;
    }
}
